package com.shf.taobao.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.shf.film.exception.ServiceException;
import com.shf.taobao.Service;

import flex.messaging.FlexContext;

/**
 * 淘宝统一入口
 * 
 * @author liucf
 *
 */
public class ServiceImpl implements Service{

	public static  ThreadLocal<String> SESSIONKEY = new ThreadLocal<String>();
	
	public static String url = "http://gw.api.taobao.com/router/rest";
	
	public static String appKey="12465280";
	
	public static String secret="50c75230b5e933cfa6cd82b2d8b00bc9";
	
	@Override
	public Object execute(String service,String methodName,String sessionKey,Object... params)throws ServiceException{
		Class<?> partypes[] = new Class[params.length ];
		Object tempParams[] = new Object[params.length];
		SESSIONKEY.set(sessionKey);
		for (int i = 0; i <params.length ; i++) {
			if(params[i] == null) params[i] = "";
			partypes[i] = params[i].getClass();
			tempParams[i] = params[i];
		}
		Object serviceObj = getBeans(service);
		Class<?> cls = serviceObj.getClass();
		try {
			Method method = cls.getMethod(methodName, partypes);
			return method.invoke(serviceObj, tempParams);
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	public Object getBeans(String service){
		ApplicationContext context =
			    new ClassPathXmlApplicationContext("../config/application-service-taobao-context.xml");
		return context.getBean(service);
	}
}
